<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\CompanyManagementModel;
use App\Models\SupplierModel;
use Illuminate\Http\Request;
use App\Services\OSS;
use Encore\Admin\Widgets\Table;
use App\Models\PurchaseOrderDetailModel;

class CommonController extends Controller
{

    public function oss_upload(Request $request){
        //获取上传的文件
        $file = $request->file('upload');
        //获取上传图片的临时地址
        $tmppath = $file->getRealPath();
        //生成文件名

        $fileName = rand(1000,9999) . $file->getFilename() . time() .date('ymd') . '.' . $file->getClientOriginalExtension();

        //拼接上传的文件夹路径(按照日期格式1810/17/xxxx.jpg)
        $pathName = date('Y-m/d').'/'.$fileName;

        //上传图片到阿里云OSS
        OSS::publicUpload('guanchi', $pathName, $tmppath, ['ContentType' => $file->getClientMimeType()]);

        //获取上传图片的Url链接
        $Url = OSS::getPublicObjectURL('guanchi', $pathName);

        return $Url;

    }

    public function download(Request $Request)
    {
        $type = $Request->get('type');
        $excel_name = "";
        if($type == 'merchant'){
            $excel_name = '网点导入模板.xlsx';
        }
        return response()->download(realpath(base_path('public')).'/import_template/'.$excel_name, $excel_name);
    }
}
